package grape.attend.service.group.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import grape.attend.service.classe.po.Classe;
import grape.attend.service.group.po.Group;
import grape.attend.service.group.po.GroupClasseRel;
import grape.attend.service.user.dto.GroupAndClasseDto;
import grape.attend.service.user.dto.GroupAndClassesDto;
import grape.common.service.common.IBaseRelService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 考勤组与班次关系表，一对多 服务类
 * </p>
 *
 * @author yangwei
 * @since 2020-05-08
 */
public interface IGroupClasseRelService extends IBaseRelService<GroupClasseRel> {

    /**
     * 根据用户id获取当前时间的适用考勤组和班次
     * @param userId
     * @return 按班次的开始时间升序排序的集合
     */
    List<GroupAndClasseDto> getGroupAndClasseCurrentByUserId(String userId);

    /**
     * 获取下一个班次，如果不同的组分配了同一个班次，视为同一个班次
     * @param group
     * @param classe
     * @param groupAndClasseDtos 按班次的开始时间升序排序的集合
     * @return
     */
    default GroupAndClasseDto getNextGroupAndClasse(Group group, Classe classe,List<GroupAndClasseDto> groupAndClasseDtos) {
        GroupAndClasseDto groupAndClasseDto = null;
        int currentClasseIndex = 0;
        int size = groupAndClasseDtos.size();
        for (int i = 0; i < size; i++) {
            groupAndClasseDto = groupAndClasseDtos.get(i);
            if (isEqual(groupAndClasseDto.getClasse().getId(),classe.getId())) {
                currentClasseIndex = i;
            }
        }
        if(currentClasseIndex == size - 1){
            currentClasseIndex = 0;
        }else {
            currentClasseIndex += 1;
        }
        return groupAndClasseDtos.get(currentClasseIndex);
    }

    /**
     * 获取上一个班次，如果不同的组分配了同一个班次，视为同一个班次
     * @param group
     * @param classe
     * @param groupAndClasseDtos 按班次的开始时间升序排序的集合
     * @return
     */
    default GroupAndClasseDto getPreGroupAndClasse(Group group, Classe classe,List<GroupAndClasseDto> groupAndClasseDtos) {
        GroupAndClasseDto groupAndClasseDto = null;
        int currentClasseIndex = 0;
        int size = groupAndClasseDtos.size();
        for (int i = 0; i < size; i++) {
            groupAndClasseDto = groupAndClasseDtos.get(i);
            if (isEqual(groupAndClasseDto.getClasse().getId(),classe.getId())) {
                currentClasseIndex = i;
            }
        }
        if(currentClasseIndex == 0){
            currentClasseIndex = size-1;
        }else {
            currentClasseIndex -= 1;
        }
        return groupAndClasseDtos.get(currentClasseIndex);
    }
    /**
     * 根据用户id获取当前时间的适用考勤组和班次
     * 同getGroupAndClasseCurrentByUserId 只是返回数据结构不同
     * @param userId
     * @return
     */
    List<GroupAndClassesDto> getGroupAndClassesCurrentByUserId(String userId);
    /**
     * 获取考勤组绑定的班次
     * @param groupId
     * @return
     */
    default List<GroupClasseRel> getByGroupId(String groupId){
        if (groupId == null) {
            return null;
        }
        return list(Wrappers.<GroupClasseRel>lambdaQuery().eq(GroupClasseRel::getGroupId,groupId));
    }

    /**
     * 清空考勤组下的班次
     * @param groupId
     * @return
     */
    default boolean removeByGroupId(String groupId){
        assertParamNotEmpty(groupId,"groupId is not empty");
        return remove(Wrappers.<GroupClasseRel>lambdaQuery().eq(GroupClasseRel::getGroupId, groupId));
    }
    /**
     * 考勤组绑定班次
     * @param groupId 考勤组id
     * @param checkedClasseIds 已选择的班次id
     * @param uncheckedClasseIds 未选择的班次id
     * @param isLazyLoad 标识页面的可选择数据是否为懒加载
     */
    default void groupAssignClasse(String groupId, List<String> checkedClasseIds,List<String> uncheckedClasseIds,Boolean isLazyLoad){

        removeAssignRel(groupId,checkedClasseIds,uncheckedClasseIds,isLazyLoad,GroupClasseRel::getGroupId,GroupClasseRel::getClasseId);
        // 再添加
        doBind(groupId, checkedClasseIds, true);
    }

    /**
     * 获取班次绑定的考勤组
     * @param classeId
     * @return
     */
    default List<GroupClasseRel> getByClasseId(String classeId){
        if (classeId == null) {
            return null;
        }
        return list(Wrappers.<GroupClasseRel>lambdaQuery().eq(GroupClasseRel::getClasseId,classeId));
    }
    /**
     * 清空班次下的考勤组
     * @param classeId
     * @return
     */
    default boolean removeByClasseId(String classeId){
        assertParamNotEmpty(classeId,"classeId is not empty");
        return remove(Wrappers.<GroupClasseRel>lambdaQuery().eq(GroupClasseRel::getClasseId, classeId));
    }
    /**
     * 班次绑定考勤组
     * @param classeId
     * @param checkedGroupIds
     * @param uncheckedGroupIds
     */
    default void classeAssignGroup(String classeId, List<String> checkedGroupIds,List<String> uncheckedGroupIds,Boolean isLazyLoad){
        removeAssignRel(classeId,checkedGroupIds,uncheckedGroupIds,isLazyLoad,GroupClasseRel::getClasseId,GroupClasseRel::getGroupId);
        // 再添加
        doBind(classeId, checkedGroupIds, false);
    }

    /**
     * 添加已选数据
     * @param mainId
     * @param checkedIds
     * @param isGroupMain
     */
    default void doBind(String mainId,List<String> checkedIds,boolean isGroupMain){
        if (!isEmpty(checkedIds)) {
            List<GroupClasseRel> insert = new ArrayList<>(checkedIds.size());
            GroupClasseRel groupClasseRel = null;
            for (String checkedId : checkedIds) {
                groupClasseRel = new GroupClasseRel();
                if (isGroupMain) {
                    groupClasseRel.setGroupId(mainId);
                    groupClasseRel.setClasseId(checkedId);
                }else {
                    groupClasseRel.setGroupId(checkedId);
                    groupClasseRel.setClasseId(mainId);
                }
                insert.add(groupClasseRel);
            }
            saveBatch(insert);
            List<String> groupIds = new ArrayList<>();
            if (isGroupMain) {
                groupIds.add(mainId);
            }else {
                groupIds.addAll(checkedIds);
            }
            checkClasse(groupIds);
        }

    }

    /**
     * 检查每一个group的班次时间是否有交叉
     */
    void checkClasse( List<String> groupIds);
}
